package com.vq3.digitalizationsoftwareintegrates.Utils.ImageUtil;

import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_Point_ContentEntity;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.CompletableFuture;

@Configuration
@Import({S_C_Point_ContentEntity.class})
/**多线程
 * */

public class CalculationIsNull {
    /**
     * 数据清洗
     *
     */
    public List<S_C_Point_ContentEntity> M_CalculationIsNull(List<String> M_thisNull, List<MultipartFile> M_files, List<String> M_a_row_col_inArrName,List<String> funArr,List<String> benchmarkArr) throws IOException {
        //使用LinkedHashMap是为了提升foreach的效率
        /**
         *用for循环arrayList 10万次花费时间：5毫秒
         * 用foreach循环arrayList 10万次花费时间：7毫秒
         * 用for循环linkList 10万次花费时间：4481毫秒
         * 用foreach循环linkList 10万次花费时间：5毫秒
         * 实际测出的20条执行时间为0毫秒
         * */

        //将文件集合传入哈希表
        LinkedHashMap<Integer, MultipartFile> M_filesHashMap = new LinkedHashMap<>();
        //将空文件标记集合传入哈希表
        LinkedHashMap<Integer, String> M_thisNullHashMap = new LinkedHashMap<>();
        //将项目名集合传入哈希表
        LinkedHashMap<Integer, String> M_a_row_col_inArrNameHashMap = new LinkedHashMap<>();
        //将点检方法传入哈希表
        LinkedHashMap<Integer, String> funArrHashMap = new LinkedHashMap<>();
        //将点检基准传入哈希表
        LinkedHashMap<Integer, String> benchmarkArrHashMap = new LinkedHashMap<>();
        //创建一个对象数组
        List<S_C_Point_ContentEntity> s_c_point_contentEntities = new ArrayList<>();
        /**
         * 看看代码就明白了
         * */
        for (int i = 0; i < M_files.size(); i++) {
            M_filesHashMap.put(i + 1, M_files.get(i));
        }
        for (int i = 0; i < M_thisNull.size(); i++) {
            M_thisNullHashMap.put(i + 1, M_thisNull.get(i));
        }
        for (int i = 0; i < M_a_row_col_inArrName.size(); i++) {
            M_a_row_col_inArrNameHashMap.put(i + 1, M_a_row_col_inArrName.get(i));
            funArrHashMap.put(i+1,funArr.get(i));
            benchmarkArrHashMap.put(i+1,benchmarkArr.get(i));
        }
        int index = 1;
        int filesIndex = 1;
        for (Map.Entry<Integer, String> arrNameEntry : M_a_row_col_inArrNameHashMap.entrySet()) {
            S_C_Point_ContentEntity s_c_point_contentEntity = new S_C_Point_ContentEntity();
            int nullNum = 0;
            if ("".equalsIgnoreCase(arrNameEntry.getValue()) || arrNameEntry.getValue() == null) {
                for (Map.Entry<Integer, String> thisNullEntry : M_thisNullHashMap.entrySet()) {
                    if (!arrNameEntry.getKey().equals(Integer.parseInt(thisNullEntry.getValue()))) {
                        nullNum++;

                        if (nullNum >= M_thisNullHashMap.size()){
                            filesIndex++;
                        }
                    }
                }
            } else {
                for (Map.Entry<Integer, String> thisNullEntry : M_thisNullHashMap.entrySet()) {
                    if (arrNameEntry.getKey().equals(Integer.parseInt(thisNullEntry.getValue()))) {
                        s_c_point_contentEntity.setPointContentId(arrNameEntry.getKey());
                        s_c_point_contentEntity.setPointContent(arrNameEntry.getValue());
                        s_c_point_contentEntity.setScFun(funArrHashMap.get(index));
                        s_c_point_contentEntity.setScBenchmark(benchmarkArrHashMap.get(index));

                    } else {
                        nullNum++;
                    }
                }
                if (nullNum >= M_thisNullHashMap.size()){
                    s_c_point_contentEntity.setPointContentId(arrNameEntry.getKey());
                    s_c_point_contentEntity.setPointContent(arrNameEntry.getValue());
                    s_c_point_contentEntity.setPoint_Content_standardImage(M_filesHashMap.get(filesIndex));
                    s_c_point_contentEntity.setImgBytes(M_filesHashMap.get(filesIndex).getBytes());
                    s_c_point_contentEntity.setScFun(funArrHashMap.get(index));
                    s_c_point_contentEntity.setScBenchmark(benchmarkArrHashMap.get(index));
                    filesIndex++;

                }
                s_c_point_contentEntities.add(s_c_point_contentEntity);
            }
            index++;
        }

        return s_c_point_contentEntities;
    }
}
